﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using WMSABL;
using WMSABL.SY;
using WMSAML.SY;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.SY
{
    public partial class UpdatePwdFrm : Form
    {
        TUsers_Entity entity = new TUsers_Entity();
        TUsers_BLL bll_TUsers = new TUsers_BLL(); 
        public UpdatePwdFrm()
        {
            InitializeComponent(); 
        }

        private void UpdatePwdFrm_Load(object sender, EventArgs e)
        { 
            entity = bll_TUsers.GetEntityByID(SystemVariable.User_ID);
            this.txtLoginName.Text = entity.Username;
        } 

        private void btnOk_Click(object sender, EventArgs e)
        {
            string oldPass = this.txtOldPass.Text.Trim();
            string newPass = this.txtNewPass.Text.Trim();
            string passAgain = this.txtPassAgain.Text.Trim();
            if (oldPass == "")
            {
                this.labShow.Text = "请输入原始密码！";
                this.txtOldPass.Focus();
                return;
            }
            if (DesMd5.MD5Encrypt(oldPass) != entity.password)
            {
                this.labShow.Text = "输入原始密码不正确！";
                this.txtOldPass.Focus();
                return;
            }
            if (newPass == "")
            {
                this.labShow.Text = "请输入新密码！";
                this.txtNewPass.Focus();
                return;
            }
            if (passAgain == "")
            {
                this.labShow.Text = "请输入确认新密码！";
                this.txtPassAgain.Focus();
                return;
            }
            if (newPass != passAgain)
            {
                this.labShow.Text = "新密码和确认密码不一致！";
                this.txtPassAgain.Focus();
                return;
            }
            if (entity != null)
            {
                entity.password = DesMd5.MD5Encrypt(newPass);
                if (MessageClass.enquire("确定修改密码！") == DialogResult.OK)
                {
                    if (bll_TUsers.Update(entity))
                    {
                        if (MessageClass.enquire("密码已修改，是否退出系统重新登录！") == DialogResult.OK)
                        {
                            System.Environment.Exit(0);
                        }
                        else
                        {
                            this.labShow.Text = "提示信息！";
                            this.txtOldPass.Text = "";
                            this.txtNewPass.Text = "";
                            this.txtPassAgain.Text = "";
                        }
                    }
                    else
                    {
                        this.labShow.Text = "失败！";
                    }
                }
                this.labShow.Text = "提示信息！";
            }
        }

        #region 退出
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
        #endregion
    }
}
